package com.lp.user.service.impl;


import com.lp.common.Result;
import com.lp.common.base.BaseService;
import com.lp.common.dto.PageBase;
import com.lp.user.dao.UserMapper;
import com.lp.user.dto.User;
import com.lp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userTestService")
public class UserSeriviceImpl extends BaseService implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User getUserInfo() {
        User uf = new User();
        uf.setNickname("ffff");
        uf.setCreatedTime(new Date());
        uf.setEnabled(Byte.valueOf("1"));
        saveUserInfor(uf);
        return uf;
    }

    public Result getUserInfos(Map param) {
        Result r = new Result();
        List<User> data = userMapper.getUserInfos(param);
        r.setData(data);
        return r;

    }

    public Result getUserInfo(Integer id) {
        Result r = new Result();
        User user = userMapper.selectByPrimaryKey(id);
        r.setData(user);
        return r;

    }

    public int saveUserInfo(User dt) {

        return userMapper.insertSelective(dt);

    }

    public int saveUserInIsolatedTx(User dt) {

        return userMapper.insertSelective(dt);
    }

    public Result updateUserInfo(User user) {
        Result r = new Result();
        int i = userMapper.updateByPrimaryKeySelective(user);
        if (i > 0) {
            r.setSuccess(SUCCESS);
        } else {
            r.setSuccess(FAIL);
        }
        return r;
    }

    public Result saveUserInfor(User dt) {
        int i = saveUserInfo(dt);
        Result r = new Result();
        if (i == 0) {
            r.setSuccess(SUCCESS);
        } else {
            r.setSuccess(FAIL);
        }
        r.setData(dt);
        return r;


    }
    public Result getPage(Integer currentPage){
        Result r=new Result();
        PageBase<User> dt=new PageBase<User>();
        dt.setCurrentPage(currentPage);
        Map p=new HashMap();
        p.put("startIndex",dt.getStartIndex());
        p.put("pageSize",10);
        int count=userMapper.getCount(p);
       List<User> u= userMapper.getPageByParam(p);
       dt.setData(u);
       dt.setCount(count);
       r.setData(dt);
       return r;

    }
    public  Result getList(){
        Result r=new Result();
        //Map p=userMapper.getList();
       // r.setData(p);\
        Map p=new HashMap();
        p.put("mysql","select * from t_user where id=#{id}  ");
        p.put("id",52);
        List list=userMapper.getList(p);
        r.setData(list);
        return r;
    }
}
